﻿package  {
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.display.StageAlign;
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;
	import flash.ui.Mouse;
	
	public class ColoringBookMain extends MovieClip {
		private var bitmap:Bitmap;
		private var coloring:Coloring;
		private var cursor:Cursor;
		private static const AVAIBLE_PICTURES:int = 12;
		
		
		public function ColoringBookMain() {
			preloader.setLoaderInfo(loaderInfo);
			preloader.addEventListener(Event.COMPLETE, onPreloaderComplete);
			if (stage) init(null);
			else stage.addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(event:Event) : void {
			stage.removeEventListener(Event.ADDED_TO_STAGE, init);
			stage.align = StageAlign.TOP_LEFT;
			
			cursor = new Cursor();
			stage.addChild(cursor);
			stage.setChildIndex(cursor, stage.numChildren-1);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			Mouse.hide();
		}
		
		private function onPreloaderComplete(e:Event):void {  
             preloader.removeEventListener(Event.COMPLETE, onPreloaderComplete);
			 play();
			 stage.setChildIndex(cursor, stage.numChildren-1);
       	}
		
		private function mouseMoveHandler(event:MouseEvent):void {
			cursor.mouseEnabled = false; 
			cursor.x = event.stageX;
			cursor.y = event.stageY - cursor.height;
		}
		
		private function createChoosePicturesScreen():void {
			var picturesContainer:Sprite = new Sprite();
			picturesContainer.graphics.drawRect(0, 0, 450, 350);
			picturesContainer.x = stage.stageWidth/2 - picturesContainer.width/2;
			picturesContainer.y = stage.stageHeight / 20;
			addChild(picturesContainer);
			
			var imgInRow:int = 0;
			var imgInColumn:int = 0;
			for (var i:int = 1; i<=AVAIBLE_PICTURES; i++) {
				var className:Class = getDefinitionByName("Pic"+i.toString()) as Class;
				var b:BitmapData = new className(0, 0);
				var bmp:Bitmap = new Bitmap(b);
				bmp.width = 100; bmp.height = 100;
				var sWraper:Sprite = new Sprite();
				sWraper.graphics.drawRect(0, 0, 100, 100);
				sWraper.x = (imgInRow)*110 + 10;
				sWraper.y = (imgInColumn)*110 + 10;
				sWraper.addChild(bmp);
				picturesContainer.addChild(sWraper);
				imgInRow++;
				if (sWraper.x + 2*sWraper.width > picturesContainer.width) {
					imgInRow = 0;
					imgInColumn++;
				}
			}
			addPicturesListeners(picturesContainer);
		}
		
		private function addPicturesListeners(pc:Sprite):void {
			for (var i:int = 0; i<pc.numChildren; i++) {
				pc.getChildAt(i).addEventListener(MouseEvent.CLICK, pictureChoosen);
				pc.getChildAt(i).addEventListener(MouseEvent.MOUSE_OVER, pictureOver);
				pc.getChildAt(i).addEventListener(MouseEvent.MOUSE_OUT, pictureOut);
			}
		}
		
		private function pictureChoosen(event:MouseEvent):void {
			if (event.target.numChildren > 0) {
				if (bitmap == null)
					bitmap = new Bitmap();
				bitmap.bitmapData = Bitmap(event.target.getChildAt(0)).bitmapData.clone();
			}
			createColoringScreen();
			stage.setChildIndex(cursor, stage.numChildren-1);
		}
		
		private function pictureOver(event:MouseEvent):void {
			event.target.width += 10;
			event.target.height += 10;
		}
		
		private function pictureOut(event:MouseEvent):void {
			event.target.width -= 10;
			event.target.height -= 10;
		}
		
		private function createColoringScreen():void {
			coloring = new Coloring(bitmap);
			addChild(coloring);
			coloring.addEventListener("Close", closeColoringScreen);
		}
		
		private function closeColoringScreen(event:Event):void {
			coloring.removeEventListener("Close", closeColoringScreen);
			removeChild(coloring);
			coloring = null;
		}
	}
}